package com.app.bimo.module_accounts.mvp.ui.activity;

import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.app.bimo.commonbusiness.base.BaseActivity;
import com.app.bimo.commonbusiness.util.Constant;
import com.app.bimo.commonbusiness.util.DataUtil;
import com.app.bimo.commonbusiness.util.RouterHub;
import com.app.bimo.commonbusiness.util.SharedPreUtils;
import com.app.bimo.commonbusiness.util.SystemUtil;
import com.app.bimo.library_db.UserData;
import com.app.bimo.library_db.helper.UserHelper;
import com.app.bimo.module_accounts.R;
import com.app.bimo.module_accounts.mvp.constract.PreferenceContract;
import com.app.bimo.module_accounts.mvp.model.entiy.PreferenceData;
import com.app.bimo.module_accounts.mvp.model.model.PreferenceUserModel;
import com.app.bimo.module_accounts.mvp.persenter.PreferencePresenter;
import com.app.bimo.module_accounts.mvp.ui.minterface.UserPreferAdapterClickListener;
import com.app.bimo.module_accounts.mvp.ui.view.UserPreferView;

import java.util.List;

import static com.app.bimo.commonbusiness.base.BaseApplication.getContext;

/**
 * 用户偏好设置
 */
@Route(path = RouterHub.LOGIN_PREFERENCE)
public class PreferenceActivity extends BaseActivity<PreferencePresenter> implements View.OnClickListener, PreferenceContract.View{

    private ImageView men;
    private ImageView women;
    private ImageView menCheck;
    private ImageView womenCheck;
    private TextView settingBtn;
    private TextView submitBtnHint;
    private TextView womenTv, menTv;
    private View menCheckBg, womenCheckBg;
    private UserPreferView preferView;

    private int channel = 1;
    private Toast toast;
    Bundle bundle = new Bundle();

    @Autowired(name = Constant.ChangeUserData)
    String changeUserData;

    @Autowired(name = Constant.isRegister)
    int isRegister = 0;
    /**
     * 上个页面告诉它是否是阻塞的 有没有头部  可不可以返回
     */
    @Autowired(name = Constant.PreferenceHaveTop)
    boolean preferenceHaveTop = false;

    /**
     * 上个页面告诉它是否是阻塞的 有没有头部  可不可以返回
     */
    @Autowired(name = Constant.LastPage)
    String LastPage;
    /**
     * 上个页面告诉它是否是阻塞的 有没有头部  可不可以返回
     */
    @Autowired(name = Constant.LastLoginPage)
    String lastLoginPage;


    @Autowired(name = Constant.BundleNovelid)
    String novelid;


    @Autowired(name = Constant.BundleChapterInt)
    int chaptetInt = -1;


    @Autowired(name = Constant.BundleChapterIntHave)
    boolean haveChapterInt = false;

    @Autowired(name = Constant.BundleChapterId)
    String chapterid;


    @Override
    protected void initView() {
//        if (!SystemUtil.isNetworkAvailable(getContext())) {
//            showError();
//        }
        mPresenter = new PreferencePresenter(new PreferenceUserModel(), this);
        men = getView(R.id.men);
        menCheck = getView(R.id.menCheck);
        menTv = getView(R.id.menTv);
        womenTv = getView(R.id.womenTv);
        womenTv = getView(R.id.womenTv);
        menCheckBg = getView(R.id.menCheckBg);
        womenCheckBg = getView(R.id.womenCheckBg);
        women = getView(R.id.women);
        womenCheck = getView(R.id.womenCheck);
        preferView = getView(R.id.userPrefer);
        men.setOnClickListener(this);
        women.setOnClickListener(this);
        settingBtn = getView(R.id.settingBtn);
        submitBtnHint = getView(R.id.submitBtnHint);
        settingBtn.setOnClickListener(this);
        womenCheckBg.setOnClickListener(this);
        menCheckBg.setOnClickListener(this);
        settingBtn.setEnabled(false);
        initToolbar();
//        hideLoading();


    }



    @Override
    protected void initData() {
        if (!preferenceHaveTop) {
            SharedPreUtils.getInstance(getContext()).putBoolean(Constant.settingPreNotAllowBack, true);
        }

        UserData userData = UserHelper.getsInstance().findUser();
        if (userData != null) {
            channel = userData.getChannel();
            if (channel != 1 && channel != 2) {
                channel = 1;
            }
            setChooseSexUI(channel == 1);
            mPresenter.getPreFromModel(channel);
        }
    }

    /**
     * 根据性别修改ui
     * @param isMen   是否男性
     */
    private void setChooseSexUI(boolean isMen) {
        if (isMen) {
            menCheck.setImageDrawable(getResources().getDrawable(R.drawable.ac_check));
            womenCheck.setImageDrawable(getResources().getDrawable(R.drawable.ac_uncheck));
            menTv.setTextColor(getResources().getColor(R.color.blue_38a6fe));
            womenTv.setTextColor(getResources().getColor(R.color.g838A96));
        } else {
            menCheck.setImageDrawable(getResources().getDrawable(R.drawable.ac_uncheck));
            womenCheck.setImageDrawable(getResources().getDrawable(R.drawable.ac_check));
            womenTv.setTextColor(getResources().getColor(R.color.blue_38a6fe));
            menTv.setTextColor(getResources().getColor(R.color.g838A96));
        }
    }


    @Override
    protected void initEvent() {
        preferView.setListener(new UserPreferAdapterClickListener() {
            @Override
            public void click() {
                if (preferView.getChooseSize() > 0) {
                    settingBtn.setEnabled(true);
                } else {
                    settingBtn.setEnabled(false);
                }
            }
        });
    }

    private void initToolbar() {
        mtoolbar = getView(R.id.toolbar);
        TextView title = getView(R.id.title);
        title.setText("设置您的阅读频道偏好");
        if (preferenceHaveTop) {
            submitBtnHint.setVisibility(View.VISIBLE);
            settingBtn.setText("下一步");
            mtoolbar.setNavigationIcon(getResources().getDrawable(R.drawable.back));
            mtoolbar.setNavigationOnClickListener(v -> {
//                ARUtil.navigationUp(getView());
            });
        } else {
            submitBtnHint.setVisibility(View.GONE);
            settingBtn.setText("开启阅读之旅");
            mtoolbar.setNavigationOnClickListener(null);
            mtoolbar.setNavigationIcon(null);
        }
    }


    @Override
    protected int setLayoutID() {
        return R.layout.account_preference_fragment;
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (id == R.id.men || id == R.id.menCheckBg) {
            setChooseSexUI(true);
            channel = 1;
            mPresenter.getPreFromModel(channel);
        }
        if (id == R.id.women || id == R.id.womenCheckBg) {
            setChooseSexUI(false);
            channel = 2;
            mPresenter.getPreFromModel(channel);
        }

        if (id == R.id.settingBtn) {
            if (preferenceHaveTop) {
                mPresenter.setPreFromModel(channel, preferView.getChooseString(), isRegister);
//                ARUtil.navigationUp(getView());
//                bundle.putString(Constant.BundleData, preferView.getChooseString());
//                bundle.putInt(Constant.BundleChannel, channel);
//                ARUtil.navigationFragment(RouterHub.LOGIN_PREFERENCENEXT, getView());
            } else {
                mPresenter.setPreFromModel(channel, preferView.getChooseString(), isRegister);
            }
        }
    }

    /**
     * 获取当前的偏好设置标签回调
     * @param list
     */
    @Override
    public void getPreDataNotify(List<PreferenceData> list) {
        preferView.setList(list);
    }

    /**
     * 设置偏好标签完成回调
     */
    @Override
    public void setPreDataNotify() {
        SharedPreUtils.getInstance(getContext()).putBoolean(Constant.settingPreSucess, true);
        if (!preferenceHaveTop) {
            showMessage("阅读偏好设置成功");
        }
        UserData data = UserHelper.getsInstance().findUser();
        if (data != null) {
            data.setChannel(channel);
            //更新男女频
            UserHelper.getsInstance().updateUser(data);
        }
        back();
//        if (!preferenceHaveTop) {
//            new Handler().postDelayed(() -> {
//                if (toast != null) {
//                    toast.cancel();
//                }
//                back();
//            }, 300);
//        } else {
//            back();
//        }
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        finish();
    }

    private void back() {
        if (true) finish();
        if (!DataUtil.isEmpty(LastPage)) {
//            ARUtil.navigationUp(getView());
//            ARUtil.navigationFragment(RouterHub.BOOKRACK_MAIN, getView());
            return;
        }

        if (!DataUtil.isEmpty(changeUserData)) {
            bundle.putString(Constant.ChangeUserData, "back");
            if (changeUserData.equals(RouterHub.LOGIN_MAIN)) {
//                ARUtil.navigationUp(getView());
                if (!DataUtil.isEmpty(lastLoginPage)) {
                    if (lastLoginPage.equals(RouterHub.USER_MINE)) {
                        SharedPreUtils.getInstance(getContext()).putString(Constant.LastLoginPage, Constant.LastLoginPage);
                    } else if (lastLoginPage.equals(RouterHub.READ_BOOK)) {
//                        backToRead();
                    }
                }
                return;
            } else if (changeUserData.equals(RouterHub.LOGIN_PREFERENCE)) {
//                ARUtil.navigationUp(getView());
//                ARUtil.navigationUp(getView());
//                ARUtil.navigationUp(getView());
                if (preferenceHaveTop) {
//                    ARUtil.navigationFragment(RouterHub.LOGIN_PREFERENCENEXT, getView());
                }
                return;
            } else {
//                ARUtil.navigationUp(getView(), bundle, changeUserData);
                if (preferenceHaveTop) {
//                    ARUtil.navigationFragment(RouterHub.LOGIN_PREFERENCENEXT, getView());
                }
                return;
            }
        } else {
            if (preferenceHaveTop) {
//                ARUtil.navigationFragment(RouterHub.LOGIN_PREFERENCENEXT, getView());
            } else {
//                ARUtil.navigationUp(getView());
            }
            return;
        }
    }

}
